﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIWSystem.Attributes
{
    /// <summary>
    /// 自定义整数范围验证特性类
    /// </summary>
    public class RangeAttribute : BaseValidateAttribute
    {
        public RangeAttribute(string description,int max,int min)
        {
            _description = description;
            _max = max;
            _min = min;
        }
        private string _description;
        /// <summary>
        /// 验证不通过的提示信息
        /// </summary>
        public override string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private int _max;
        /// <summary>
        /// 最大值
        /// </summary>
        public int Max
        {
            get { return _max; }
            set { _max = value; }
        }
        private int _min;
        /// <summary>
        /// 最小值
        /// </summary>
        public int Min
        {
            get { return _min; }
            set { _min = value; }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="value">值</param>
        /// <returns></returns>
        public override bool Validate(object value)
        {
            int intValue = 0;
            //先检查传递过来的值是否整数
            var result= int.TryParse(value.ToString(), out intValue);
            if (result)
            {
                return intValue >= Min && intValue <= Max;
            }
            else
            {
                return false;
            }
        }
    }
}
